<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 'add' ? '新增商品分类' : '编辑商品分类'"
    :visible.sync="visible"
    width="700px"
    @closed="reset"
  >

    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="right" label-width="100px">
      <el-form-item label="上级分类:" style="width: 100%;" prop="parentName">
        <el-input v-model="temp.parentName" disabled />
      </el-form-item>
      <el-form-item label="分类编码:" prop="gtCode">
        <el-input v-model="temp.gtCode" :disabled="type !== 'add'" placeholder="请输入">
          <el-button v-if="type==='add'" slot="append" class="append-search" @click.prevent="getCode">获取</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="分类名称:" prop="gtName">
        <el-input v-model="temp.gtName" :maxlength="20" clearable placeholder="请输入" />
      </el-form-item>
      <el-form-item label="主营业务:" prop="gtFlag">
        <el-select v-model="temp.gtFlag" placeholder="请选择">
          <el-option label="主营业务-液化气" :value="1" />
          <el-option label="主营业务-气瓶" :value="2" />
          <el-option label="非主营业务" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品类型:" prop="gtCategory">
        <el-select v-model="temp.gtCategory" placeholder="请选择">
          <el-option v-for="item in gtCategoryMap" :key="item.value" :label="item.desc" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="显示顺序:" prop="showIndex">
        <el-input-number v-model="temp.showIndex" style="width: 100%;" :min="1" :max="999" :precision="0" />
      </el-form-item>
      <el-form-item label="责任归属:" prop="specialFlag">
        <el-select v-model="temp.specialFlag" placeholder="请选择">
          <el-option label="当前公司" :value="-1" />
          <el-option label="美气多公司" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注:" prop="remark" class="block">
        <el-input v-model="temp.remark" type="textarea" :rows="3" maxlength="255" show-word-limit />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { goodsTypeQueryListAdd, queryMap, queryOrgCode } from '@/api'
  import { mergeAsyncFunc } from '@/utils/mergeAsyncFunc'

  export default {
    name: 'add',
    data() {
      return {
        visible: false,
        type: 'add',
        gtCategoryMap: [],
        gtId: 0,
        temp: {
          parentName: '',
          gtCode: '',
          gtName: '',
          gtFlag: 1,
          gtCategory: 1,
          showIndex: undefined,
          remark: '',
          specialFlag: -1
        },
        rules: {
          gtName: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
          showIndex: [{ required: true, message: '请输入显示顺序', trigger: 'blur' }]
        }
      }
    },
    created() {
      this.queryOptionsMap()
    },
    methods: {
      open(temp, type, treeData) {
        this.type = type
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (type === 'edit') {
            if (temp) {
              const parent = treeData.find(item => item.gtId === temp.parentId)
              this.temp = Object.assign(this.temp, temp)
              if (parent) this.temp.parentName = parent.gtName
            }
          } else {
            if (temp) this.temp.parentName = temp.gtName
          }
          if (temp) this.gtId = temp.gtId
        })
      },
      // 获取编码
      async getCode() {
        const res = await queryOrgCode({
          codeType: 8
        })
        this.temp.gtCode = res.data
      },
      // 查询枚举
      async queryOptionsMap() {
        const res = await mergeAsyncFunc(queryMap, { enumName: 'GoodsTypeCategory' })
        this.gtCategoryMap = res.data // 商品类型
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {
            gtCode: this.temp.gtCode,
            gtName: this.temp.gtName,
            gtFlag: this.temp.gtFlag,
            gtCategory: this.temp.gtCategory,
            remark: this.temp.remark,
            showIndex: this.temp.showIndex,
            specialFlag: this.temp.specialFlag
          }
          if (this.type === 'edit') {
            params.gtId = this.gtId // 更新时用到，新增时自动生成
          } else {
            params.parentId = this.gtId
          }
          if (this.type === 'add') {
            this.add(params)
          } else {
            this.edit(params)
          }
        })
      },
      async add(params) {
        await goodsTypeQueryListAdd(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '新增成功',
          type: 'success',
          duration: 2000
        })
      },
      async edit(params) {
        await goodsTypeQueryListAdd(params)
        this.resolve(this.temp)
        this.visible = false
        this.$notify({
          title: '提示',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.type = 'add'
        this.gtId = 0
        this.temp = {
          gtCode: '',
          gtName: '',
          gtFlag: 1,
          gtCategory: 1,
          showIndex: undefined,
          remark: '',
          specialFlag: -1
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 50%;
    padding-right: 10px;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
</style>
